package com.lcx.plugin.neteaseCloud.util;

import com.lcx.plugin.neteaseCloud.constant.StringConstant;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.Objects;
import java.util.logging.Logger;

/**
 * @author JackuXL
 */
public class NetWorkUtil {

    private static final Logger log = Logger.getLogger(NetWorkUtil.class.getName());

    public static String domain = StringConstant.BASE_URL;

    /**
     * GET请求
     *
     * @param url    地址（删除主域名）
     * @param cookie Cookie（可为Null）
     * @return 请求结果
     */
    public static String sendByGetUrl(String url, String cookie) {
        if (cookie == null) {
            cookie = "";
        }
        String requestUrl = NetWorkUtil.domain + url;
        log.info("发送请求：" + requestUrl);
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        Request request = new Request.Builder()
                .addHeader("Cookie", cookie)
                .url(requestUrl)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String result = Objects.requireNonNull(response.body()).string();
            log.info("获取响应：" + result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}